package com.newway.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.newway.common.result.Result;
import com.newway.entity.Notice;
import com.newway.mapper.NoticeMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Resource
    NoticeMapper noticeMapper;

    @GetMapping("/list")
    public Result getList(Notice notice){
        Page<Notice> noticePage = new Page<>(notice.getPageNum(), notice.getPageSize());
        QueryWrapper<Notice> noticeQueryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(notice.getTitle())){
            noticeQueryWrapper.like("title",notice.getTitle());
        }
        noticeQueryWrapper.eq("status","1");
        noticeQueryWrapper.orderByDesc("is_first");
        noticeQueryWrapper.orderByDesc("id");
        Page<Notice> noticePage1 = noticeMapper.selectPage(noticePage, noticeQueryWrapper);
        return Result.success(noticePage1);
    }

    @GetMapping("/detail/{id}")
    public Result detail(@PathVariable("id")Integer id){
        Notice notice = noticeMapper.selectById(id);
        if(notice!=null){
            notice.setViewCount(notice.getViewCount()+1);
            noticeMapper.updateById(notice);
        }
        return Result.success(notice);
    }

    @GetMapping("/latestNotice")
    public Result latestNotice(){
        QueryWrapper<Notice> noticeQueryWrapper = new QueryWrapper<>();
        noticeQueryWrapper.orderByDesc("id");
        noticeQueryWrapper.eq("status","1");
        noticeQueryWrapper.last("limit 3");
        List<Notice> notices = noticeMapper.selectList(noticeQueryWrapper);
        return Result.success(notices);
    }



    @GetMapping("/listAdmin")
    public Result listAdmin(Notice notice){
        Page<Notice> noticePage = new Page<>(notice.getPageNum(), notice.getPageSize());
        QueryWrapper<Notice> noticeQueryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(notice.getTitle())){
            noticeQueryWrapper.like("title",notice.getTitle());
        }
        noticeQueryWrapper.orderByDesc("is_first");
        noticeQueryWrapper.orderByDesc("id");
        Page<Notice> noticePage1 = noticeMapper.selectPage(noticePage, noticeQueryWrapper);
        return Result.success(noticePage1);
    }

    @GetMapping("/delete/{id}")
    public Result delete(@PathVariable("id")Integer id){
        noticeMapper.deleteById(id);
        return Result.success();
    }

    @PostMapping("/addOrUpdate")
    public Result addOrUpdate(@RequestBody Notice notice){
        if(notice.getId()==null){
            noticeMapper.insert(notice);
        }else{
            Notice notice1 = noticeMapper.selectById(notice.getId());
            notice1.setContent(notice.getContent());
            notice1.setSummary(notice.getSummary());
            notice1.setTitle(notice.getTitle());
            noticeMapper.updateById(notice1);
        }
        return Result.success();
    }

    @PostMapping("/changeStatus/{id}/{status}")
    public Result changeStatus(@PathVariable("id")Integer id,@PathVariable("status")String status){
        Notice notice = noticeMapper.selectById(id);
        if(notice!=null){
            notice.setStatus(status);
            noticeMapper.updateById(notice);
        }
        return Result.success();
    }
}
